package com.hangwei.paperless.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hangwei.paperless.R;
import com.hangwei.paperless.model.bean.Disposition;
import com.hangwei.paperless.model.bean.ResultBean;
import com.twelve.all.utils.Tools;

import java.util.List;

/**
 * Created by 张龙臣 on 2020/12/8.
 */

public abstract class DispositionAdapter extends RecyclerView.Adapter {

    private List<Disposition> mDispositionList;
    private Context mContext;

    public DispositionAdapter(List<Disposition> dispositionList) {
        mDispositionList = dispositionList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mContext = viewGroup.getContext();
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.pop_item_home_choose, viewGroup, false);
        return new RecyclerView.ViewHolder(inflate) {
        };
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ImageView ivChecked = viewHolder.itemView.findViewById(R.id.ivChecked);
        TextView tvTypeName = viewHolder.itemView.findViewById(R.id.tvTypeName);
        final Disposition sheet = mDispositionList.get(i);
        boolean hasChecked = false;
        for (Disposition bean : mDispositionList) {
            if (bean.isChecked)
                hasChecked = true;
        }
        if (!hasChecked)
            mDispositionList.get(0).isChecked = true;
        ivChecked.setVisibility(sheet.isChecked ? View.VISIBLE : View.INVISIBLE);
        tvTypeName.setTextColor(sheet.isChecked ? mContext.getResources().getColor(R.color.blue1) : mContext.getResources().getColor(R.color.text));
        tvTypeName.setText(Tools.checkedString(sheet.number + ": " + sheet.name));
        final int position = i;
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mDispositionList.get(position).isChecked) {
                    for (Disposition sh : mDispositionList) {
                        sh.isChecked = false;
                    }
                    mDispositionList.get(position).isChecked = true;
                }
                onAdapterItemClick(position);
                notifyDataSetChanged();
            }
        });
    }

    protected abstract void onAdapterItemClick(int position);

    @Override
    public int getItemCount() {
        return mDispositionList == null ? 0 : mDispositionList.size();
    }
}
